
package com.yunbao.core.spring.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * AOP日志
 * </p>
 *
 * @author 安能
 * @date 2019年1月10日
 */

@Slf4j
@Aspect
@Component
public class WebLogAspect {


    @Pointcut(value = "execution(public * com.yunbao.project.basedata.controller.*.*(..))")
    public void webLog() {
    }

    /**
     * <p>
     * 方法拦截之前开始打印日志
     * </p>
     *
     * @param joinPoint
     */
    @Before(value = "webLog()")
    public void beBefore(JoinPoint joinPoint) {

        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
        request.setAttribute("CURRENT_METHOD", joinPoint.getSignature().getName());
    }


    @Around(value = "webLog()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {

        return joinPoint.proceed();
    }
    /**
     * <P>
     * 打印方法返回的结果值
     * </p>
     *
     * @param object
     */
    @AfterReturning(returning = "object", pointcut = "webLog()")
    public void doAfterReturning(Object object) {
//        log.error(object.toString());
    }
}

